package com.aiju.cash.ui.setting;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.bean.Rate;
import com.aiju.cash.R;
import com.aiju.cash.core.api.RateApi;
import com.aiju.cash.core.api.SimpleCallback;
import com.aiju.cash.util.GsonHelper;
import com.aiju.widgit.layout.SettingItemTextView;

import java.util.List;

/**
 * @author: 佐为
 * @date: 2016-12-19 17:52
 * @version: V1.0
 * @Description: 费率
 */
public class RateActivity extends BaseTitleActivity {

    private ListView rateListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate);
        setCenterTitle("费率");

        rateListView = (ListView) findViewById(R.id.lv_rate);
        findViewById(R.id.ly_rate_info).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(RateInfoActivity.class);
            }
        });
        requestRateList();
    }

    /**
     * 请求费率列表
     */
    private void requestRateList() {
        mDialogHelper.showLoadingProgressDialog();
        new RateApi().dealRateList(mUser.getToken(), new SimpleCallback(mDialogHelper) {
            @Override
            protected void onDataSuccess(String data) {
                List<Rate> rates = GsonHelper.convertEntities(data, Rate.class);
                rateListView.setAdapter(new MyBaseAdapter(RateActivity.this, rates));
            }
        });
    }

    private final class MyBaseAdapter extends BaseAdapter {

        private Context context;
        private List<Rate> rates;

        public MyBaseAdapter(Context context, List<Rate> rates) {
            this.context = context;
            this.rates = rates;
        }

        @Override
        public int getCount() {
            return rates == null ? 0 : rates.size();
        }

        @Override
        public Object getItem(int position) {
            return rates.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            SettingItemTextView rootView = (SettingItemTextView) LayoutInflater.from(context).inflate(R.layout.item_rate, parent, false);
            Rate clerk = rates.get(position);
            rootView.setLeftText(clerk.getType());
            rootView.setRightText(clerk.getRate());
            return rootView;
        }
    }
}
